<?php
App::uses('AppController', 'Controller');

class FrontEndController extends AppController {

	public $theme = 'FrontEnd';
	
	public function index() {
		$this->layout = 'home';
	}
	
	public function tour(){
		$this->layout = 'tour';
	}

	public function pricing(){
		$this->layout = 'pricing';
	}

	public function gallery(){
		$this->layout = 'gallery';
	}

	public function blog(){
		$this->layout = 'blog';
	}

	public function about(){
		$this->layout = 'about';
	}
	
	public function login(){
		$this->layout = 'login';
	}

	public function signup(){
		$this->layout = 'signup';
	}

	public function tool(){
		$this->layout = 'tool';
	}

	public function add_img(){
		$ds = DIRECTORY_SEPARATOR;  //1
		$storeFolder = 'img/images/uploads/';   //2
		 
		if (!empty($_FILES)) {
		     
		    $tempFile = $_FILES['file']['tmp_name'];          //3             
		      
		    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  //4
		     
		    $targetFile =  $targetPath. $_FILES['file']['name'];  //5
		 
		    move_uploaded_file($tempFile,$targetFile); //6
		     
		} else {                                                           
		    $result  = array();
		 
		    $files = scandir($storeFolder);                 //1
		    if ( false!==$files ) {
		        foreach ( $files as $file ) {
		            if ( '.'!=$file && '..'!=$file) {       //2
		                $obj['name'] = $file;
		                $obj['size'] = filesize($storeFolder.$ds.$file);	
		                $result[] = $obj;
		            }
		        }
		    }
		     
		    header('Content-type: text/json');              //3
		    header('Content-type: application/json');
		    echo json_encode($result);
		}
		die();
		
	}

	public function delete_img(){
		$upload_dir = 'img/images/uploads';

		$targetPath = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . $upload_dir . DIRECTORY_SEPARATOR;

		unlink($targetPath.$_POST['id']);
		die();
	}
	
	public function parse_vimeo_json(){
		$imgid = $_POST['id'];
		$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$imgid.php"));
		$img_url_vimeo = $hash[0]['thumbnail_large'];/*thumbnail_medium thumbnail_small thumbnail_large*/ 

		$src_img = @imagecreatefromjpeg($img_url_vimeo); 
		$img_name = md5($imgid);
		imagejpeg($src_img, "img/images/uploads/{$img_name}.jpg");
		imagedestroy($src_img);
		echo $image_url = "img/images/uploads/{$img_name}.jpg";
		die();
	}

	public function parse_youtube_json(){
		$imgid = $_POST['id'];
		$img_url_youtube = "https://img.youtube.com/vi/$imgid/0.jpg";

		$src_img = @imagecreatefromjpeg($img_url_youtube); 
		$img_name = md5($imgid);
		imagejpeg($src_img, "img/images/uploads/{$img_name}.jpg");
		imagedestroy($src_img);
		echo $image_url = "img/images/uploads/{$img_name}.jpg";
		die();
	}

	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow();
	}
}
